package org.apache.cordova.splashscreen;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.graphics.Color;
import android.text.Spannable;

import com.makerworks.medu.R;

/**
 * Created by louis on 2017/1/13.
 */

public class CustomDialog extends Dialog {
    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;

        public Builder(Context context) {
            this.context = context;
        }

        public CustomDialog create(String title, String message, String left, String right, final DialogInterface.OnClickListener okListener, final DialogInterface.OnClickListener cancelListener) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomDialog dialog = new CustomDialog(context, R.style.Dialog);
            dialog.setCanceledOnTouchOutside(false);
            View layout = inflater.inflate(R.layout.dialog_normal_layout, null);

            SpannableString spanString = new SpannableString(message);
            ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#00B9FF"));
            int index = message.indexOf(title);
            spanString.setSpan(span, index, index + title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            ((TextView) layout.findViewById(R.id.message)).setText(spanString);


            ((Button) layout.findViewById(R.id.okButton)).setText(left);
            ((Button) layout.findViewById(R.id.okButton)).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    okListener.onClick(dialog,
                            DialogInterface.BUTTON_POSITIVE);
                }
            });
            ((Button) layout.findViewById(R.id.cancelButton)).setText(right);
            ((Button) layout.findViewById(R.id.cancelButton)).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    cancelListener.onClick(dialog,
                            DialogInterface.BUTTON_POSITIVE);
                }
            });
            dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);
            return dialog;
        }
    }
}
